package com.yndsoft.jc.ui.categorypage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.Product;
import com.yndsoft.jc.ui.categorypage.entity.ProductListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/12/5.
 */

public class ProductListAdapter extends BaseAdapter {

    private List<Product> datas = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;

    public ProductListAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setDatas(List<Product> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int postion, View convertView, ViewGroup viewGroup) {
        ProductListViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_prodct_list, null);
            holder = new ProductListViewHolder();
            holder.product_image = convertView.findViewById(R.id.product_image);
            holder.product_title = convertView.findViewById(R.id.product_title);
            holder.product_price = convertView.findViewById(R.id.product_price);
            convertView.setTag(holder);
        } else {
            holder = (ProductListViewHolder) convertView.getTag();
        }

//        holder.product_image.setImageResource(datas.get(postion).getImageRes());
        Glide.with(context).load(datas.get(postion).getImg()).placeholder(R.mipmap.logo).into(holder.product_image);
        holder.product_title.setText(datas.get(postion).getSubname());
        holder.product_price.setText("￥" + datas.get(postion).getPrice() + "");

        return convertView;
    }

    class ProductListViewHolder {
        ImageView product_image;
        TextView product_title, product_price;
    }
}
